package com.sky.loveshop.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.Sku;
import com.sky.loveshop.entity.aliPay.OrderRequest;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.PrivateOrderRequest;
import com.sky.loveshop.entity.qo.SkuQO;
import com.sky.loveshop.entity.qo.StockParam;
import com.sky.loveshop.entity.qo.StockRequest;
import com.sky.loveshop.entity.vo.SkuVO;
import com.sky.loveshop.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:04
 */
@Slf4j
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @PostMapping("/listSkuPages")
    public Result<PageResult<SkuVO>> listSkuPages(@RequestBody SkuQO skuQO) {
        return Result.page(skuService.listSkuPages(skuQO));
    }

    @PostMapping("/listSku")
    public Result<List<SkuVO>> listSku(@RequestBody SkuQO skuQO) {
        return Result.success(skuService.listSku(skuQO));
    }

    @PutMapping("/addSku")
    public Result<Integer> addSku(@RequestBody Sku sku) {
        return Result.success(skuService.addSku(sku));
    }

    @DeleteMapping("/delSkuById")
    public Result<Integer> delSkuById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(skuService.delSkuById(id));
    }

    @PostMapping("/editSku")
    public Result<Integer> editSku(@RequestBody Sku sku) throws BizException {
        if (sku.getId() == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(skuService.updateSkuById(sku));
    }

    @GetMapping("/getSkuById")
    public Result<SkuVO> getSkuById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(skuService.getSkuById(id));
    }

    @PostMapping("/changeStockNum")
    public Result<Integer> changeStockNum(@RequestBody @Valid StockParam stockParam) throws BizException {
        log.info("changeStockNumParam={}", JSONUtil.toJsonStr(stockParam));
        return Result.success(skuService.changeStockNum(stockParam));
    }

    /**
     * 根据shopId查询库存告警商品列表(库存管理APP使用)
     * @param req
     * @return
     */
    @PostMapping("/getDangerNumberPageByShopId")
    public Result<PageResult<SkuVO>> getDangerNumberPageByShopId(@RequestBody @Valid StockRequest.Q1 req) throws BizException {
        log.info("getDangerNumberPageByShopIdParam={}", JSONUtil.toJsonStr(req));
        if (null == req.getShopId()) {
            throw new BizException(BizExceptionEnum.SHOP_ID_MUST_NEED);
        }
        return Result.page(skuService.getDangerNumberList(req));
    }

    /**
     * 根据用户关联的商店查询所有商店的库存告警商品列表(后台管理系统使用)
     * @param req
     * @return
     */
    @PostMapping("/getDangerNumberPageByShopIds")
    public Result<PageResult<SkuVO>> getDangerNumberPage(@RequestBody @Valid StockRequest.Q1 req) throws BizException {
        log.info("getDangerNumberPageByShopIdsParam={}", JSONUtil.toJsonStr(req));
        if (CollectionUtil.isEmpty(req.getShopIds())) {
            throw new BizException(BizExceptionEnum.SHOP_IDS_MUST_NEED);
        }
        return Result.page(skuService.getDangerNumberList(req));
    }

    /**
     * 根据用户选择的商店和商品名称模糊查询SKU(供私域用户输入商品名称下单)使用
     */
    @PostMapping("/getSkuVOByShopIdAndName")
    public Result<List<SkuVO>> getSkuVOByShopIdAndName(@RequestBody @Valid PrivateOrderRequest.Q1 req) throws BizException {
        log.info("getSkuVOByShopIdAndName={}", JSONUtil.toJsonStr(req));
        return Result.success(skuService.getSkuVOByShopIdAndName(req));
    }

}
